/**
 * 
 */
Ext.setup({
    tabletStartupScreen: 'tablet_startup.png',
    phoneStartupScreen: 'phone_startup.png',
    icon: 'tablet_startup.png',
    glossOnIcon: false,
    onReady : function() {
    	console.log("DOM ready");
    	Ext.get('loadingDiv').remove();
    }
});

/**
 * @ignore
 * @method
 */
Ext.regApplication({
    name: 'app',
    launch: function() {
        this.launched = true;
        this.mainLaunch();
    },
	useLoadMask: true,
	
	/**
	 * {NG.database}
	 */
	database:null,
	
	profiles: {
		phone: function() {
			return Ext.is.Phone;
		},
		tabletPortrait: function() {
			return Ext.is.Tablet && Ext.orientation == 'portrait';
		},
		tabletLandscape: function() {
			return Ext.is.Tablet && Ext.orientation == 'landscape';
		}
	},				   
    mainLaunch: function() {
        if (!(typeof device === "undefined") && (!device || !this.launched)) {
			return;
		}
        NGLogger.info('mainlaunch');
        this.views.viewport = new NG.views.Viewport();
    },
    
    getViewport: function () {
    	return this.views.viewport;
    },
    
    createLogo: function () {
    	 var img = document.createElement("img");
         img.src = "img/netcad.png";
         img.width = 133;
         img.height = 40;
         img.style = "float:right";
         return img; 	
    },
    
    /**
     * 
     * @returns {String} logo html string which will be displayed on map
     */
    mapLogoHtml: function () {
    	var data = {
    		html: "http://www.netcad.com.tr/",
    		width: Ext.is.Phone ? "40%" : "70%",
    		imgUrl: "img/netcad.png",
    		title: 'Netcad'
    	};
    	var tpl = new Ext.XTemplate(
    			'<table>',
    			'<tr>',
    			'<td align="right">',
    	    	'<a href="{html}" target="_blank"><img src="{imgUrl}" width="{width}" title="{title}"></a>',    	
    	    	'</td>',
    	    	'</tr>',
    	    	'</table>'
    	);
    	return tpl.apply(data);
    },
    
    /**
     * Default settings values
     */
    DEFAULT_SETTINGS: {
		'id'	  : 1,
        'name'    : 'İsim',
        'password': 'secret',
        'email'   : 'ng@netgis.com.tr',
        'url'     : 'http://213.139.195.74/webgis_net',
        'language': 'TR',
        'tileSize': 256,
        'useCache': true,
        'cacheTimeout': 30,
        'showZoomControl': Ext.is.Desktop || Ext.is.Android,
        'offline': false,
        'lineColor': '0000FF' //blue
    },
    
    /**
     * 
     * @returns settings {NG.models.Settings} object
     */
    getSettings: function (){
    	if (NG.stores.settings.getCount() == 1) {
    		return NG.stores.settings.getAt(0);
    	} else {
    		return Ext.ModelMgr.create(this.DEFAULT_SETTINGS, 'NG.models.Settings');
    	}
    },
    
    getEnvironmentHTML: function (){
    	var html = null;
		if (typeof device !== "undefined")
	        html = 
	        					'Device Name: '     + device.name     + '<br />' + 
	                            'Device PhoneGap: ' + device.phonegap + '<br />' + 
	                            'Device Platform: ' + device.platform + '<br />' + 
	                            'Device UUID: '     + device.uuid     + '<br />' + 
	                            'Device Version: '  + device.version  + '<br />';
		else 
			html = 'PhoneGap: <b>device</b> object not supported';	
		
		html += "<br/><br/>";
		html += "Sencha Version: " + Ext.version + "<br/>";
		html += "OpenLayers Version: " + OpenLayers.VERSION_NUMBER + "<br/>";
		html += "NetGis Version: " + NG.platformVersion + "<br/>";
		html += "Touch Support: " + Ext.supports.Touch + "<br/>";
		html += "OrientationChange Support: " + Ext.supports.OrientationChange + "<br/>";
		html += "GeoLocation Support: " + Ext.supports.GeoLocation + "<br/>";
		return html;
    }
});

Ext.Ajax.loading = "<table><tr><td align='center'><img src='app/resources/loading.gif'><td></tr></table>";
Ext.Ajax.loadingOverLay = new Ext.Panel({
    floating: true,
    modal: false,
    width: 60,
    height: 60,
    border: "0 0 0 0",
    bodyBorder: false,
    //styleHtmlContent: true,
    layout:'fit',
    hideOnMaskTap: false,
    //contentEl: 'ngloading',
    html: "<img src='app/resources/loading.gif'>",
    cls: 'htmlcontent'
//    items:[{
//    	html: Ext.Ajax.loading
//    }]
});
Ext.Ajax.on('beforerequest', function(connection, options){
	var message = L_LOADING;
	if (options && !Ext.isEmpty(options.message)) {
		message = options.message;
	}
	//Ext.getBody().mask(loading);
	//Ext.get('ng-loading-div').show();
	
	if (options.showLoading == true)
		Ext.Ajax.loadingOverLay.show();
});

Ext.Ajax.on('requestcomplete', function(connection, options){
	if (Ext.Ajax.loadingOverLay.isVisible()) {
		Ext.getBody().unmask();
		Ext.Ajax.loadingOverLay.hide();		
	}
});

Ext.Ajax.on('requestexception', function(connection,options){
	//Ext.get('ng-loading-div').hide();
	Ext.getBody().unmask();
	Ext.Ajax.loadingOverLay.hide();
});



/**
Synchronous request.  
  
@param {Object} method
@param {Object} uri
@param {Object} callback
@param {Object} postData
*/
Ext.Ajax.syncRequest = function(method, uri, callback, postData)
{
    var o = this.getConnectionObject();

    if (!o) {
        return null;
    }
    else {
        o.conn.open(method, uri, false);

        if (this.useDefaultXhrHeader) {
            if (!this.defaultHeaders['X-Requested-With']) {
                this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
            }
        }

        if(postData && this.useDefaultHeader && (!this.hasHeaders || !this.headers['Content-Type'])){
            this.initHeader('Content-Type', this.defaultPostHeader);
        }

        if (this.hasDefaultHeaders || this.hasHeaders) {
            this.setHeader(o);
        }

        o.conn.send(postData || null);
        this.handleTransactionResponse(o, callback);
        return o;
    }
};

/*
var task = {
    run: function(){
        NGLogger.info(new Date().format('g:i:s A'));
    },
    interval: 2000 //1 second
}
Ext.TaskMgr.start(task);*/